//
//  SkylowProfileCharaterViewController.swift
//  SkylowAPP
//
//  Created by 青色石头 on 2024/9/22.
//

import UIKit
import JXSegmentedView

class SkylowProfileCharaterViewController: UIViewController {
    
    private lazy var emptyLbl = {
        let lbl = UILabel()
        lbl.text = "No characters."
        lbl.textColor = .white.withAlphaComponent(0.8)
        lbl.font = .systemFont(ofSize: 12, weight: .light)
        self.view.addSubview(lbl)
        lbl.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(16)
            make.leading.equalToSuperview().offset(24)
        }
        return lbl
    }()
    
    private lazy var collectionView = {
        let flowLayout = UICollectionViewFlowLayout()
        flowLayout.sectionInset = UIEdgeInsets(top: 12, left: 0, bottom: 12, right: 0)
        let vw = UICollectionView(frame: .zero, collectionViewLayout: flowLayout)
        vw.backgroundColor = .black
        vw.delegate = self
        vw.dataSource = self
        
        vw.register(UINib(nibName: "SkylowProfileCharaterCell", bundle: nil), forCellWithReuseIdentifier: "character_cell")
        self.view.addSubview(vw)
        vw.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        return vw
    }()
    
    
    private var datas: [String] = ["ceshi", "测试1"] {
        didSet {
            collectionView.isHidden = datas.count == 0
            emptyLbl.isHidden = datas.count != 0
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        _ = emptyLbl
        _ = collectionView
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        let itemSizeWidth: CGFloat = view.width - 24 - 24
        let itemHeight: CGFloat = 122
        let itemSize = CGSize(width: itemSizeWidth, height: itemHeight)
        let flowLayout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
        if !flowLayout.itemSize.equalTo(itemSize) {
            flowLayout.itemSize = itemSize
        }
    }

}

extension SkylowProfileCharaterViewController: UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        datas.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "character_cell", for: indexPath)
        return cell
    }
}

extension SkylowProfileCharaterViewController: UICollectionViewDelegate {
    
}

extension SkylowProfileCharaterViewController: JXSegmentedListContainerViewListDelegate {
    func listView() -> UIView {
        view
    }
}
